package com.dz.risk.service.impl;

import cn.afterturn.easypoi.excel.ExcelExportUtil;
import cn.afterturn.easypoi.excel.entity.ExportParams;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.dz.risk.common.SysConstant;
import com.dz.risk.common.utils.ResponseUtils;
import com.dz.risk.dao.ProprietaryValuationOfBondHoldingsInformationDao;
import com.dz.risk.pojo.QueryParams;
import com.dz.risk.pojo.domain.ProprietaryValuationOfBondHoldingsInformationEntity;
import com.dz.risk.pojo.domain.RiskFactorBacktracking;
import com.dz.risk.service.ProprietaryValuationOfBondHoldingsInformationService;
import org.apache.poi.hssf.util.HSSFColor;
import org.apache.poi.ss.usermodel.*;
import org.apache.poi.xssf.streaming.SXSSFWorkbook;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.util.CollectionUtils;
import org.springframework.util.StringUtils;

import javax.servlet.ServletOutputStream;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.net.URLEncoder;
import java.util.List;


@Service("proprietaryValuationOfBondHoldingsInformationService")
public class ProprietaryValuationOfBondHoldingsInformationServiceImpl extends ServiceImpl<ProprietaryValuationOfBondHoldingsInformationDao, ProprietaryValuationOfBondHoldingsInformationEntity> implements ProprietaryValuationOfBondHoldingsInformationService {
    @Autowired
    private ProprietaryValuationOfBondHoldingsInformationDao proprietaryValuationOfBondHoldingsInformationDao;

    @Override
    public IPage<ProprietaryValuationOfBondHoldingsInformationEntity> query(QueryParams queryParams) {
        IPage<ProprietaryValuationOfBondHoldingsInformationEntity> page = new Page<>();
        page.setCurrent(queryParams.getPageNum());
        page.setSize(queryParams.getPageSize());
        QueryWrapper<ProprietaryValuationOfBondHoldingsInformationEntity> wrapper = new QueryWrapper<ProprietaryValuationOfBondHoldingsInformationEntity>();
        wrapper.lambda()
                .eq(!StringUtils.isEmpty(queryParams.getInitDate()),ProprietaryValuationOfBondHoldingsInformationEntity::getInitDate,queryParams.getInitDate())
                .eq(!StringUtils.isEmpty(queryParams.getStockCode()),ProprietaryValuationOfBondHoldingsInformationEntity::getStockCode,queryParams.getStockCode())
                .like(!StringUtils.isEmpty(queryParams.getAbbreviationOfSecurities()),ProprietaryValuationOfBondHoldingsInformationEntity::getAbbreviationOfSecurities,queryParams.getAbbreviationOfSecurities());
        return proprietaryValuationOfBondHoldingsInformationDao.selectPage(page, wrapper);
    }

    @Override
    public ProprietaryValuationOfBondHoldingsInformationEntity queryByBondCode(String bondCode) {
        QueryWrapper<ProprietaryValuationOfBondHoldingsInformationEntity> wrapper = new QueryWrapper<ProprietaryValuationOfBondHoldingsInformationEntity>();

        if (StringUtils.hasLength(bondCode) && !String.valueOf(SysConstant.ZERO).equals(bondCode)) {
            wrapper.eq("stock_code", bondCode);
        }
        return proprietaryValuationOfBondHoldingsInformationDao.selectOne(wrapper);
    }

    @Override
    public List<ProprietaryValuationOfBondHoldingsInformationEntity> queryByVersion(Long version) {
        QueryWrapper<ProprietaryValuationOfBondHoldingsInformationEntity> wrapper = new QueryWrapper<ProprietaryValuationOfBondHoldingsInformationEntity>();
        wrapper.lambda().eq(ProprietaryValuationOfBondHoldingsInformationEntity::getInitDate,version);
        return proprietaryValuationOfBondHoldingsInformationDao.selectList(wrapper);
    }

    @Override
    public void doExportByVersion(HttpServletResponse response, QueryParams queryParams) throws IOException {
        SXSSFWorkbook wb = new SXSSFWorkbook();
        Sheet sh = wb.createSheet();
        /*
            日期
            证券代码
            证券简称
            减值金额
            全价市值
         */
        String[] titles = {"日期", "证券代码", "证券简称", "减值金额","全价市值"};
        Row row = sh.createRow(0);
        CellStyle style = wb.createCellStyle();
        style.setFillForegroundColor(HSSFColor.HSSFColorPredefined.WHITE.getIndex());
        style.setFillPattern(FillPatternType.SOLID_FOREGROUND);
        style.setBorderBottom(BorderStyle.THIN);
        style.setBorderLeft(BorderStyle.THIN);
        style.setBorderRight(BorderStyle.THIN);
        style.setBorderTop(BorderStyle.THIN);
        style.setAlignment(HorizontalAlignment.CENTER);

        // 第一行设置标题
        for (int i = 0; i < titles.length; i++) {
            String title = titles[i];
            Cell cell1 = row.createCell(i);
            cell1.setCellValue(title);
            cell1.setCellStyle(style);
            //设置单元格大小
            sh.setColumnWidth(i, 6000);
        }

        if (queryParams.getInitDate().contains("-")){
            queryParams.setInitDate(queryParams.getInitDate().replace("-",""));
        }
        QueryWrapper<ProprietaryValuationOfBondHoldingsInformationEntity> wrapper = new QueryWrapper<ProprietaryValuationOfBondHoldingsInformationEntity>();
        wrapper.lambda()
                .eq(!StringUtils.isEmpty(queryParams.getInitDate()),ProprietaryValuationOfBondHoldingsInformationEntity::getInitDate,queryParams.getInitDate())
                .eq(!StringUtils.isEmpty(queryParams.getStockCode()),ProprietaryValuationOfBondHoldingsInformationEntity::getStockCode,queryParams.getStockCode())
                .like(!StringUtils.isEmpty(queryParams.getAbbreviationOfSecurities()),ProprietaryValuationOfBondHoldingsInformationEntity::getAbbreviationOfSecurities,queryParams.getAbbreviationOfSecurities());
        List<ProprietaryValuationOfBondHoldingsInformationEntity> proprietaryValuationOfBondHoldingsInformationEntityList = proprietaryValuationOfBondHoldingsInformationDao.selectList(wrapper);
        final String fileName = "自营估值债券持仓信息_%s.xlsx";
        ServletOutputStream outputStream = ResponseUtils.getOutputStreamAndSettingFileFormat(response, fileName);
        //如果该版本得风险因子回溯有数据则填充进Excel
        if (!CollectionUtils.isEmpty(proprietaryValuationOfBondHoldingsInformationEntityList)) {
            Workbook sheets = ExcelExportUtil.exportExcel(new ExportParams(), ProprietaryValuationOfBondHoldingsInformationEntity.class, proprietaryValuationOfBondHoldingsInformationEntityList);
            sheets.write(outputStream);
            outputStream.close();
            sheets.close();
        }
        response.setHeader("Content-Disposition", "attachment;filename=" + URLEncoder.encode(fileName, "UTF-8"));
        wb.write(response.getOutputStream());
        wb.close();
    }


}